#include <bmp.h>
#include <lcd.h>
#include <album.h>
#include <wuzi.h>
#include <unistd.h>
#include <LinkStack.h>
 #define  R 50
#define  M  12 
#define  N  20 

int wuzigame[M][N] = {0}; //初始化棋盘数据
int color = 1; //用来确定棋子颜色
int flag =0; //判断棋子能否下
int music=0; //判断音乐是否播放
int x, y;  //坐标
int dx, dy;  //用来存储悔棋的数据坐标
//保存整个游戏的数据，0空，1黑，2白
LinkStack * s;
int wuzi()
{
	display_row(0,0,800,600,0x000000);
	display_col (0,0,800,600,0x000000);
	show_bmp(425,50,"quit.bmp");
    show_bmp(425,250,"re.bmp");
	show_bmp(425,450,"music_start.bmp");
	s=InitStack();
    while(1)
    {
	int ree=show_bmp(487,0,"666.bmp");
    get_touch( &x, &y );
	if((x>=974&&x<1024)&&(y>=0&&y<=50))
	{	
	return 0;
	}
	anjian();
    chess(x,y);
	if (panduan() == 1)
	{
	printf("黑棋获胜");
	show_bmp( 0, 0, "win.bmp" );
	break;
	}
	if (panduan() == 2)
	{
	printf("白棋获胜");
	show_bmp( 0, 0, "game_over.bmp" );
	break;
	}
    }

}

void chess( int x ,int y)
{
    if (chargechess(x, y) == 0) return;
    if(color==1)
    {
    wuzigame[y/R][x/R] = 1;     //黑棋
    display_Circle( (x/R)*R + R/2, (y/R)*R + R/2, R/2, 0x000000 );  
	Push(s,x,y);
    }
    else
    {
     wuzigame[y/R][x/R] = 2;     //白棋
    display_Circle( (x/R)*R + R/2, (y/R)*R + R/2, R/2, 0xffffff );    
	Push(s,x,y);
    }
    color=-color;
}

int chargechess(int x, int y) { //判断是否有棋子或者超出范围
	if (wuzigame[y/R][x/R] == 0&&x<=780) {
		return 1;
	}
	 else return 0;
}

int win(int x, int y) {//判断是否连了5个字,返回一则为黑棋胜利，二为白棋胜利。
	if (wuzigame[x][y] == 0) return 0;
	if (wuzigame[x][y] == wuzigame[x - 4][y] && wuzigame[x][y] == wuzigame[x - 3][y] && wuzigame[x][y] == wuzigame[x - 2][y] && wuzigame[x][y] == wuzigame[x - 1][y])
		if (wuzigame[x][y] == 1)return 1; else return 2;//左
	if (wuzigame[x][y] == wuzigame[x + 1][y] && wuzigame[x][y] == wuzigame[x + 2][y] && wuzigame[x][y] == wuzigame[x + 3][y] && wuzigame[x][y] == wuzigame[x + 4][y])
		if (wuzigame[x][y] == 1)return 1; else return 2;//右
	if (wuzigame[x][y] == wuzigame[x][y + 4] && wuzigame[x][y] == wuzigame[x][y + 3] && wuzigame[x][y] == wuzigame[x][y + 2] && wuzigame[x][y] == wuzigame[x][y + 1])
		if (wuzigame[x][y] == 1)return 1; else return 2;;//上
	if (wuzigame[x][y] == wuzigame[x][y - 1] && wuzigame[x][y] == wuzigame[x][y - 2] && wuzigame[x][y] == wuzigame[x][y - 3] && wuzigame[x][y] == wuzigame[x][y - 4])
		if (wuzigame[x][y] == 1)return 1; else return 2;;//下
	if (wuzigame[x][y] == wuzigame[x - 1][y - 1] && wuzigame[x][y] == wuzigame[x - 2][y - 2] && wuzigame[x][y] == wuzigame[x - 3][y - 3] && wuzigame[x][y] == wuzigame[x - 4][y - 4])
		if (wuzigame[x][y] == 1)return 1; else return 2;;//左上
	if (wuzigame[x][y] == wuzigame[x + 1][y - 1] && wuzigame[x][y] == wuzigame[x + 2][y - 2] && wuzigame[x][y] == wuzigame[x + 3][y - 3] && wuzigame[x][y] == wuzigame[x + 4][y - 4])
		if (wuzigame[x][y] == 1)return 1; else return 2;;//左下
	if (wuzigame[x][y] == wuzigame[x - 1][y + 1] && wuzigame[x][y] == wuzigame[x - 2][y + 2] && wuzigame[x][y] == wuzigame[x - 3][y + 3] && wuzigame[x][y] == wuzigame[x - 4][y + 4])
		if (wuzigame[x][y] == 1)return 1; else return 2;;//右上
	if (wuzigame[x][y] == wuzigame[x + 1][y + 1] && wuzigame[x][y] == wuzigame[x + 2][y + 2] && wuzigame[x][y] == wuzigame[x + 3][y + 3] && wuzigame[x][y] == wuzigame[x + 4][y + 4])
		if (wuzigame[x][y] == 1)return 1; else return 2;//右下
	return 0;
}

int panduan() {
	int i, j;
	for (i = 0; i < N; i++) {
	for (j = 0; j < N; j++) {
		if (win(i, j) == 1) return 1;
		if (win(i, j) == 2) return 2;
	}
}
	return 0;
}



int anjian()
{
	if((x>=850&&x<950)&&(y>=50&&y<=150))
	{
	show_pure_color( 0xFFFF00 );  		
	wuzi();
	}
	if((x>=850&&x<950)&&(y>=250&&y<=350))
	{	
	huiqi();
	}
	if((x>=850&&x<950)&&(y>=450&&y<=550))
	{
	if(music==0)
	{
		system("madplay -Q  123.mp3  &");
		show_bmp(425,450,"music_psuse.bmp");
		music=1;
	}
	else if(music==1)
	{
		 system( "killall  -9 madplay" );
		 show_bmp(425,450,"music_start.bmp");
		 music=0;
	}
	}
	

}


int huiqi()
{
	hq * pnew = (hq *)malloc( sizeof(hq) );
	if(Pop(s,pnew))
	if(wuzigame[pnew->y/R][pnew->x/R]!=0)
	{
	wuzigame[pnew->y/R][pnew->x/R] = 0; 
	printf("huiqi %d huiqi %d\n",pnew->x,pnew->y);
	huiqitianchong(pnew->x,pnew->y);
	}
}
	